package org.ruoyi.dhuman.service.impl;

import org.ruoyi.common.core.utils.MapstructUtils;
    import org.ruoyi.core.page.TableDataInfo;
    import org.ruoyi.core.page.PageQuery;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.ruoyi.dhuman.domain.bo.DhumanPptProgressBo;
import org.ruoyi.dhuman.domain.vo.DhumanPptProgressVo;
import org.ruoyi.dhuman.domain.DhumanPptProgress;
import org.ruoyi.dhuman.mapper.DhumanPptProgressMapper;
import org.ruoyi.dhuman.service.IDhumanPptProgressService;
import org.ruoyi.common.core.utils.StringUtils;

import java.util.List;
import java.util.Collection;

/**
 * 数字人讲PPT视频合成进度Service业务层处理
 *
 * @author wangliang
 * @date Tue Oct 14 12:06:25 CST 2025
 */
@RequiredArgsConstructor
@Service
public class DhumanPptProgressServiceImpl implements IDhumanPptProgressService {

    private final DhumanPptProgressMapper baseMapper;

    /**
     * 查询数字人讲PPT视频合成进度
     */
    @Override
    public DhumanPptProgressVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

	/**
	 * 查询数字人讲PPT视频合成进度列表
	 */
	@Override
	public TableDataInfo<DhumanPptProgressVo> queryPageList(DhumanPptProgressBo bo, PageQuery pageQuery) {
		LambdaQueryWrapper<DhumanPptProgress> lqw = buildQueryWrapper(bo);
		Page<DhumanPptProgressVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
		return TableDataInfo.build(result);
	}

    /**
     * 查询数字人讲PPT视频合成进度列表
     */
    @Override
    public List<DhumanPptProgressVo> queryList(DhumanPptProgressBo bo) {
        LambdaQueryWrapper<DhumanPptProgress> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DhumanPptProgress> buildQueryWrapper(DhumanPptProgressBo bo) {
        LambdaQueryWrapper<DhumanPptProgress> lqw = Wrappers.lambdaQuery();
                    lqw.eq(bo.getId() != null, DhumanPptProgress::getId, bo.getId());
                    lqw.eq(StringUtils.isNotBlank(bo.getTenantId()), DhumanPptProgress::getTenantId, bo.getTenantId());
                    lqw.eq(StringUtils.isNotBlank(bo.getDhumanPptId()), DhumanPptProgress::getDhumanPptId, bo.getDhumanPptId());
                    lqw.eq(StringUtils.isNotBlank(bo.getProgressLog()), DhumanPptProgress::getProgressLog, bo.getProgressLog());
                    lqw.eq(bo.getCreateTime() != null, DhumanPptProgress::getCreateTime, bo.getCreateTime());
                    lqw.eq(StringUtils.isNotBlank(bo.getRemark()), DhumanPptProgress::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增数字人讲PPT视频合成进度
     */
    @Override
    public Boolean insertByBo(DhumanPptProgressBo bo) {
        DhumanPptProgress add = MapstructUtils.convert(bo, DhumanPptProgress. class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改数字人讲PPT视频合成进度
     */
    @Override
    public Boolean updateByBo(DhumanPptProgressBo bo) {
        DhumanPptProgress update = MapstructUtils.convert(bo, DhumanPptProgress. class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DhumanPptProgress entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除数字人讲PPT视频合成进度
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
